<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import {
  Menu as IconMenu,
  HomeFilled,
  Reading,
  User,
  Setting,
  DataLine,
  ChatDotRound,
  ArrowDown
} from '@element-plus/icons-vue'
import Header from '@/components/Header.vue'

const router = useRouter()
const userStore = useUserStore()

// 侧边栏折叠状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = ref('dashboard')

const showUserMenu = ref(false)

// 新菜单项结构，支持分组和子菜单
const menuItems = [
  {
    index: 'analytics',
    title: '数据分析',
    icon: DataLine
  },
  {
    index: 'course-classify',
    title: '课程分类管理',
    icon: DataLine
  },
  {
    index: 'courses',
    title: '课程管理',
    icon: Reading
  },
  {
    index: 'community-group',
    title: '社区管理',
    icon: User,
    children: [
      {
        index: 'community',
        title: '文章管理',
        icon: User
      },
      {
        index: 'messages',
        title: '文章分类管理',
        icon: ChatDotRound
      }
    ]
  },
  {
    index: 'settings',
    title: '系统设置',
    icon: Setting
  }
]

// 处理菜单点击
const handleMenuClick = (index) => {
  activeMenu.value = index
  router.push(`/teacher/${index}`)
}

const goToProfile = () => router.push('/profile')
const goToMyArticles = () => router.push('/my-articles')
const goToMain = () => router.push('/')

// 判断是否为老师或管理员
const isManager = computed(() => {
  const role = userStore.userInfo.role || userStore.userInfo.type || ''
  return role === 'teacher' || role === 'admin' || role === '管理员' || role === '老师'
})

const goToAdmin = () => router.push('/teacher')

// 处理登出
const handleLogout = async () => {
  try {
    await userStore.logout()
    ElMessage.success('登出成功')
    router.push('/login')
  } catch (error) {
    console.error('登出失败:', error)
    ElMessage.error('登出失败，请稍后重试')
  }
}
</script>

<template>
  <div class="teacher-dashboard">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo-container">
        <img src="/images/tubiao.jpg" alt="Logo" class="logo">
        <span v-show="!isCollapse" class="title">后台管理</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="menu"
        :collapse="isCollapse"
        @select="handleMenuClick"
      >
        <template v-for="item in menuItems" :key="item.index">
          <el-sub-menu v-if="item.children" :index="item.index">
            <template #title>
              <el-icon><component :is="item.icon" /></el-icon>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item v-for="sub in item.children" :key="sub.index" :index="sub.index">
              <el-icon><component :is="sub.icon" /></el-icon>
              <span>{{ sub.title }}</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item v-else :index="item.index">
            <el-icon><component :is="item.icon" /></el-icon>
            <span>{{ item.title }}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>

    <!-- 主要内容区 -->
    <el-container class="main-container">
      <!-- 恢复原有顶部 header -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="collapse-btn" @click="isCollapse = !isCollapse">
            <icon-menu />
          </el-icon>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click">
            <span class="user-info">
              <el-avatar :size="32" :src="userStore.userInfo.avatar">
                <el-icon><user /></el-icon>
              </el-avatar>
              <span class="username">{{ userStore.userInfo.username }}</span>
              <el-icon><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">个人中心</el-dropdown-item>
                <el-dropdown-item @click="goToMain">主界面</el-dropdown-item>
                <el-dropdown-item>我的收藏</el-dropdown-item>
                <el-dropdown-item @click="goToMyArticles">我的文章</el-dropdown-item>
                <el-dropdown-item v-if="isManager" @click="goToAdmin">管理界面</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout" style="color:#f56c6c;">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <!-- 内容区 -->
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<style scoped>
.teacher-dashboard {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;
}

.logo {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.title {
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
}

.menu {
  border-right: none;
  background-color: #304156;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.header {
  height: 60px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 30px 0 0;
  border-bottom: 1px solid #e6e6e6;
}

.header-left {
  display: flex;
  align-items: center;
  height: 100%;
}

.header-right {
  display: flex;
  align-items: center;
  height: 100%;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main {
  padding: 20px;
  overflow-y: auto;
}

:deep(.el-menu) {
  border-right: none;
  background-color: #304156;
}

:deep(.el-menu-item), :deep(.el-sub-menu__title) {
  color: #bfcbd9;
  background-color: #304156;
}

:deep(.el-menu-item.is-active), :deep(.el-menu-item:hover), :deep(.el-sub-menu__title.is-active), :deep(.el-sub-menu__title:hover) {
  color: #409eff;
  background-color: #263445;
}

:deep(.el-sub-menu .el-menu-item) {
  color: #bfcbd9;
  background-color: #263445;
}

:deep(.el-sub-menu .el-menu-item.is-active), :deep(.el-sub-menu .el-menu-item:hover) {
  color: #409eff;
  background-color: #232e3b;
}

:deep(.el-sub-menu__title) {
  font-weight: 500;
}
</style> 